
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// 火车票信息结构体
struct Train {
    string trainID;
    string startStation;
    string endStation;
    string startTime;
    string arriveTime;
    double price;
    int ticketsLeft;

    Train* next; // 链表指针
};
struct IDcard {
    string name;
    string ID;
};//乘客的个人身份

// 头指针
Train* head = nullptr;
void searchtrain() {
    if (head == nullptr) {
        cout << "当前没有任何车次信息，请先添加！" << endl;
        return;
    }

    int option;
    cout << "\n请选择查询方式：" << endl;
    cout << "1. 根据车次查询" << endl;
    cout << "2. 根据城市（始发站或终点站）查询" << endl;
    cout << "请输入选项：";
    cin >> option;

    if (option == 1) {
        string key;
        cout << "请输入要查询的车次：";
        cin >> key;

        Train* p = head;
        bool found = false;

        while (p != nullptr) {
            if (p->trainID == key) {
                cout << "\n【查询结果】" << endl;
                cout << "车次：" << p->trainID << endl;
                cout << "始发站：" << p->startStation << endl;
                cout << "终点站：" << p->endStation << endl;
                cout << "发车时间：" << p->startTime << endl;
                cout << "到达时间：" << p->arriveTime << endl;
                cout << "票价：" << p->price << endl;
                cout << "余票：" << p->ticketsLeft << endl;
                found = true;
                break;
            }
            p = p->next;
        }

        if (!found) {
            cout << "未找到该车次。" << endl;
        }
    }
    else if (option == 2) {
        string city;
        cout << "请输入城市名称（始发站或终点站）：";
        cin >> city;

        Train* p = head;
        bool found = false;

        cout << "\n【查询结果】" << endl;
        while (p != nullptr) {
            if (p->startStation == city || p->endStation == city) {
                cout << "车次：" << p->trainID << " "
                    << p->startStation << " → " << p->endStation << " "
                    << "发车：" << p->startTime << " 到达：" << p->arriveTime
                    << " 票价：" << p->price << " 剩余：" << p->ticketsLeft << endl;
                found = true;
            }
            p = p->next;
        }

        if (!found) {
            cout << "未找到包含该城市的车次。" << endl;
        }
    }
    else {
        cout << "无效选项，请输入 1 或 2。" << endl;
    }
}

void bookticket() {
    if (head == nullptr) {
        cout << "暂无火车信息，无法订票！" << endl;
        return;
    }

    string city;
    cout << "请输入目的地城市（终点站）：";
    cin >> city;

    Train* p = head;
    bool found = false;
    int index = 1;

    Train* options[100]; // 临时保存所有可订票的车次
    cout << "\n【可预订车次】" << endl;
    while (p != nullptr) {
        if (p->endStation == city && p->ticketsLeft > 0) {
            cout << index << ". 车次：" << p->trainID
                << " 发车：" << p->startTime
                << " 到达：" << p->arriveTime
                << " 票价：" << p->price
                << " 剩余票数：" << p->ticketsLeft << endl;
            options[index] = p;
            index++;
            found = true;
        }
        p = p->next;
    }

    if (!found) {
        cout << "没有可预订的车次。" << endl;
        return;
    }

    int select;
    cout << "请输入要预订的编号：";
    cin >> select;

    if (select <= 0 || select >= index) {
        cout << "输入无效！" << endl;
        return;
    }

    Train* selectedTrain = options[select];
    if (selectedTrain->ticketsLeft <= 0) {
        cout << "票已售完！" << endl;
        return;
    }

    // 输入用户信息
    IDcard passenger;
    cout << "请输入乘客姓名：";
    cin >> passenger.name;
    cout << "请输入身份证号码：";
    cin >> passenger.ID;

    // 票数减一
    selectedTrain->ticketsLeft--;

    cout << "订票成功！已为 " << passenger.name
        << " 预订了 " << selectedTrain->trainID << " 次列车。" << endl;

    fstream orderFile("orders.txt", ios::app); //保存客户车票
    if (orderFile.is_open()) {
        orderFile << passenger.name << " "
            << passenger.ID << " "
            << selectedTrain->trainID << " "
            << selectedTrain->startStation << " "
            << selectedTrain->endStation << " "
            << selectedTrain->startTime << " "
            << selectedTrain->arriveTime << " "
            << selectedTrain->price << endl;
        orderFile.close();
        cout << "乘客信息已记录到 orders.txt 文件中。" << endl;
    }
    else {
        cout << "无法写入乘客信息到 orders.txt！" << endl;
    }
}

void modify() {
    if (head == nullptr) {
        cout << "当前没有任何车次信息，请先添加！" << endl;
        return;
    }

    string trainID;
    cout << "请输入要修改的车次号：";
    cin >> trainID;

    Train* p = head;
    while (p != nullptr) {
        if (p->trainID == trainID) {
            cout << "\n找到该车次，当前信息如下：" << endl;
            cout << "车次：" << p->trainID << endl;
            cout << "始发站：" << p->startStation << endl;
            cout << "终点站：" << p->endStation << endl;
            cout << "发车时间：" << p->startTime << endl;
            cout << "到达时间：" << p->arriveTime << endl;
            cout << "票价：" << p->price << endl;
            cout << "余票：" << p->ticketsLeft << endl;

            cout << "\n请选择要修改的内容：" << endl;
            cout << "1. 始发站\n2. 终点站\n3. 发车时间\n4. 到达时间\n5. 票价\n6. 余票数量\n0. 取消修改" << endl;
            int option;
            cin >> option;

            switch (option) {
            case 1:
                cout << "新的始发站：";
                cin >> p->startStation;
                break;
            case 2:
                cout << "新的终点站：";
                cin >> p->endStation;
                break;
            case 3:
                cout << "新的发车时间：";
                cin >> p->startTime;
                break;
            case 4:
                cout << "新的到达时间：";
                cin >> p->arriveTime;
                break;
            case 5:
                cout << "新的票价：";
                cin >> p->price;
                break;
            case 6:
                cout << "新的余票数量：";
                cin >> p->ticketsLeft;
                break;
            case 0:
                cout << "取消修改。" << endl;
                return;
            default:
                cout << "无效选项。" << endl;
                return;
            }

            cout << "修改完成！" << endl;
            return;
        }

        p = p->next;
    }

    cout << "未找到该车次！" << endl;
}

void showtrain() {
    if (head == nullptr) {
        cout << "当前没有任何车次信息，请先添加！" << endl;
        return;
    }

    Train* p = head;
    int count = 1;
    cout << "\n【全部火车票信息如下】" << endl;
    while (p != nullptr) {
        cout << "\n车次 #" << count++ << endl;
        cout << "车次：" << p->trainID << endl;
        cout << "始发站：" << p->startStation << endl;
        cout << "终点站：" << p->endStation << endl;
        cout << "发车时间：" << p->startTime << endl;
        cout << "到达时间：" << p->arriveTime << endl;
        cout << "票价：" << p->price << endl;
        cout << "余票：" << p->ticketsLeft << endl;
        p = p->next;
    }
}

void savetraininfo() {
    if (head == nullptr) {
        cout << "没有信息可保存！" << endl;
        return;
    }

    ofstream outFile("traininfo.txt"); // 覆盖写入
    if (!outFile.is_open()) {
        cout << "无法打开文件进行保存！" << endl;
        return;
    }

    Train* p = head;
    while (p != nullptr) {
        outFile << p->trainID << " "
            << p->startStation << " "
            << p->endStation << " "
            << p->startTime << " "
            << p->arriveTime << " "
            << p->price << " "
            << p->ticketsLeft << endl;
        p = p->next;
    }

    outFile.close();
    cout << "信息已成功保存到 traininfo.txt 文件中。" << endl;
}


void menubar() {
    int choice;
    do {
        cout << "1.查询火车票信息" << endl;
        cout << "2.订票" << endl;
        cout << "3.显示火车票信息" << endl;
        cout << "4.完成订票，保存信息" << endl;
        cout << "0.返回上一层" << endl;
        cout << "请选择操作：";
        cin >> choice;
        switch (choice) {
        case 1:
            searchtrain();
            break;
        case 0:
            return;
        case 2:
            bookticket();
            break;
        case 3:
            showtrain();
            break;
        case 4:
            savetraininfo();
            break;
        default:
            cout << "无效选择，请重新输入。" << endl;
        }
    } while (choice != 0);
    //
}
void Traininfo() {
    Train* newTrain = new Train;

    cout << "请输入车次：";
    cin >> newTrain->trainID;

    // 检查是否重复
    Train* p = head;
    while (p != nullptr) {
        if (p->trainID == newTrain->trainID) {
            cout << "该车次已存在，添加失败。" << endl;
            delete newTrain;
            return;
        }
        p = p->next;
    }

    cout << "请输入始发站：";
    cin >> newTrain->startStation;
    cout << "请输入终点站：";
    cin >> newTrain->endStation;
    cout << "请输入发车时间：";
    cin >> newTrain->startTime;
    cout << "请输入到达时间：";
    cin >> newTrain->arriveTime;
    cout << "请输入票价：";
    cin >> newTrain->price;
    cout << "请输入余票数量：";
    cin >> newTrain->ticketsLeft;

    newTrain->next = head;
    head = newTrain;

    // 保存到文件
    ofstream outFile("traininfo.txt", ios::app); // 追加写入
    if (outFile.is_open()) {
        outFile << newTrain->trainID << " "
            << newTrain->startStation << " "
            << newTrain->endStation << " "
            << newTrain->startTime << " "
            << newTrain->arriveTime << " "
            << newTrain->price << " "
            << newTrain->ticketsLeft << endl;
        outFile.close();
        cout << "火车信息已保存。" << endl;
    }
    else {
        cout << "无法打开文件保存。" << endl;
    }
}
int main() {
    int choice;
    do {
        cout << "\n=== 火车票订票系统 ===" << endl;
        cout << "0. 退出" << endl;
        cout << "1. 工作人员添加火车信息" << endl;
        cout << "2.工作人员修改火车信息" << endl;
        cout << "3.用户入口" << endl;
        cout << "请选择操作：";
        cin >> choice;

        switch (choice) {
        case 1:
            Traininfo();
            break;
        case 0:
            cout << "退出系统。" << endl;
            break;
        case 3:
            menubar();
            break;
        case 2:
            modify();
            break;
        default:
            cout << "无效选择，请重新输入。" << endl;
        }
    } while (choice != 0);

    return 0;
}
